package com.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Songzzz
 * @Description
 * @Date 2025/2/27 17:20
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    public HelloServlet(){
        System.out.println("HelloServlet初始化");
    }

    @Override
    public  void init() throws ServletException {
        System.out.println("HELLOSERVLET INIT");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException {
        //给浏览器返回一行数据
        resp.getWriter().write("helloServlet销毁");
    }

    @Override
    public void destroy(){
        System.out.println("HelloServlet destroy");
    }

    //处理GET请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException {
        //设置请求编码
        req.setCharacterEncoding("UTF-8");
        String name=req.getParameter("name");
        String age=req.getParameter("age");
        System.out.println("name:"+name+"age"+age);
        //给浏览器返回一行数据
        resp.getWriter().write("hello！！！Servlet!!");
    }

}
